<template>
  <van-button
    class="follow-btn"
    round
    size="small"
    v-if="isFollowed"
    @click="handlerFollow"
    :loading="isLoading"
  >已关注</van-button>
  <van-button
    v-else
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    :loading="isLoading"
    @click="handlerFollow"
  >关注</van-button>
</template>

<script>
import {addFollow, deleteFollow} from '@/api/user'
export default {
  data() {
    return {
      isLoading: false
    }
  },
  model: {
    prop: 'isFollowed',
    event: 'updataIsFollowed'
  },
  props: ['isFollowed', 'autId'],
  methods: {
    async handlerFollow() {
      this.isLoading = true

      try {
        if(this.isFollowed) {
          const {data} = await deleteFollow(this.autId)
        } else {
          const {data} = await addFollow(this.autId)
        }
        
        this.$emit('updataIsFollowed', !this.isFollowed)
      } catch(err) {
        if (err.response && err.response.status === 400) {
          return this.$toast.fail('不能关注自己')
        }
        this.$toast.fail('操作失败')
      }

      this.isLoading = false
    }
  }
}
</script>

<style>

</style>